/**
 * @Author 韦建韩
 * @Date 2022/3/24
 */
package com.zczn.order_system.service.ServiceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zczn.order_system.constant.AdministratorsConstant;
import com.zczn.order_system.constant.ExceptionResponseConstant;
import com.zczn.order_system.constant.HttpCodeConstant;
import com.zczn.order_system.entity.dto.ResultDto;
import com.zczn.order_system.exception.CustomException;
import com.zczn.order_system.mapper.AdministratorsMapper;
import com.zczn.order_system.entity.Administrators;
import com.zczn.order_system.service.IService.IAdministratorInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
@Service
public class AdministratorsInfoServiceImpl implements IAdministratorInfoService {
    @Autowired
    AdministratorsMapper administratorsMapper;

    public Administrators findAllInfoByAccount(String account){
        QueryWrapper<Administrators> queryWrapper = new QueryWrapper<Administrators>();
        queryWrapper.eq(AdministratorsConstant.Field.Account.variableName, account);
        Administrators resultinfo = administratorsMapper.selectOne(queryWrapper);
        return resultinfo;
    }
    @Override
    public ResultDto findInfoByAccount(Administrators administrators) {
        ResultDto resultDto = new ResultDto();
        Administrators resultinfo = findAllInfoByAccount(administrators.getAccount());
        if (Objects.isNull(resultinfo)) {
            throw new CustomException(HttpCodeConstant.Field.AuthenticationError.code, ExceptionResponseConstant.ACCCOUNTNOEXIST);
        } else {
            if (resultinfo.getPassword().equals(administrators.getPassword())) {
                resultDto.success();
                resultDto.setData(resultinfo);
            }
            else {
                throw new CustomException(HttpCodeConstant.Field.AuthenticationError.code, ExceptionResponseConstant.PASSWORDERROR);
            }
        }
        return resultDto;
    }

    @Override
    public ResultDto insertAdministrators(Administrators administrators) {
        Administrators info = findAllInfoByAccount(administrators.getAccount());
        if(Objects.nonNull(info)){
            throw new CustomException(HttpCodeConstant.Field.MethodDisabled.code,ExceptionResponseConstant.USEREXISTED);
        }
        administratorsMapper.insert(administrators);
        return null;
    }


    @Override
    public ResultDto before(Administrators administrators) {
        return null;
    }


    @Override
    public ResultDto after(Administrators administrators) {
        return null;
    }
}
